<?php
namespace ZuJi\Controller;
use  Common\Controller\ZuJiController;
class UserController extends ZuJiController {

    public function get_config()
    {
        $input_data['app']              =  I('post.app','','htmlspecialchars');
        $input_data['version']          =  I('post.version','','htmlspecialchars');
        if (! empty($input_data['app']) && ! empty($input_data['version']))
        {
            //跟多好玩
            $extra['app_id']    =   'wx6b1c7907c5733fc9';   //朋友小聚
            $extra['path']      =   'pages/index/index';

            $more['app_id']     =   'wx6b1c7907c5733fc9';
            $more['path']       =   'pages/index/index';
            $more['name']       =   'name';

            $more['extra']      =   $extra;

            //分享
            $share['img']       =   'https://qphone.mamapai.net/album/wx/photo_share1.png';
            $share['mine']      =   '我的足迹';

            $data           =   array();
            $data['jump']   =   array();
            $data['banner'] =   array();
            $data['more']   =   $more;
            $data['version']=   100;
            $data['allow_share']    =   true;


            $this->returnData($data,'ok',0);
        }
    }
    public function add_form_id()
    {
        $openid           =  I('post.openid','','htmlspecialchars');
        $form_id          =  I('post.form_id','','htmlspecialchars');
        $share_uid        =  I('post.share_uid','','htmlspecialchars');
        if(! empty($openid) && ! empty($form_id))
        {
            $where['openid'] =   $openid;
            $field          =   array('user_id','expire_time','perfect_time','openid');
            $member_data    =   M('zuji_member')->where($where)->field($field)->find();

            if (empty($member_data))
            {
                $this->returnData('','please login update',101);
            }

            $template_id    =   1;
            $date           =   date('Y-m-d');
            $form_type      =   1;
            $insert = array(
                'appid'=>'wx8e26d39e155e28ef',
                'touser'=>$member_data['openid'],
                'template_id'=>$template_id,
                'page'=>'pages/index/index',
                'form_id'=>$form_id,
                'form_type'=>$form_type,
                'status'=>'-1',
                'add_time'=>time(),
                'secret'=>'0836bbd26264ffe013a12cadbae30925',
                'createtime'=>$date,
                'user_id'=>$member_data['user_id']
            );

            $where               = array();
            $where['touser']     = $member_data['openid'];
            $where['createtime'] = $date;
            $where['status']     = '-1';
            $ishave = M('zuji_template_send')->field('wxopen_template_send_id')->where($where)->find();
            if ($ishave)
            {
                M('zuji_template_send')->where(array('wxopen_template_send_id'=>$ishave['wxopen_template_send_id']))->save($insert);
            }
            else
            {
                M('zuji_template_send')->data($insert)->add();
            }

            if (! empty($share_uid) && $share_uid != $member_data['user_id'])
            {
                //朋友
                $this->make_friend($member_data['user_id'] ,$share_uid);
            }
        }
    }

    public function add_friend()
    {
        $input      = file_get_contents('php://input');
        $input_data = json_decode($input, true);

        if (! empty($input_data) || $_POST)
        {
            if (! empty($_POST))
            {
                $input = json_encode($_POST);
            }

            $data               =   array();
            $data['module']     =   'user';
            $data['url']        =    $_SERVER["REQUEST_SCHEME"].'://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
            $data['log']        =    $input;
            $data['create_time']=   time();
            M('yao_log')->add($data);
        }
    }

    public function login()
    {
        $code                   =  I('post.code','','htmlspecialchars');
        $encryptedData          =  I('post.encryptedData','','htmlspecialchars');
        $iv                     =  I('post.iv','','htmlspecialchars');
        $share_uid              =   I('post.share_uid','','htmlspecialchars');
        $user_data              =   array();
        $user_data['avatar']    =   I('post.avatarUrl','','htmlspecialchars');
        $user_data['city']      =   I('post.city','','htmlspecialchars');
        $user_data['gender']    =   I('post.gender','','htmlspecialchars');
        $user_data['language']  =   I('post.language','','htmlspecialchars');
        $user_data['nickname']  =   I('post.nickName','','htmlspecialchars');
        $user_data['province']  =   I('post.province','','htmlspecialchars');
        $user_data['country']   =   I('post.country','','htmlspecialchars');

        if (! empty($code) && ! empty($encryptedData)
        && ! empty($iv) && ! empty($user_data) )
        {
            $appid      =   'wx8e26d39e155e28ef';
            $secret     =   '0836bbd26264ffe013a12cadbae30925';

            $url = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";

            $res = $this->curlget($url);

            if (!$res['openid'])
            {
                $this->returnData($res,$msg='openid获取失败',$code=1001);
            }

            $user_id        =   0;
            $where          =   array();
            $where['openid']=   $res['openid'];
            $field          =   array('user_id','nickname','avatar','openid','login_num');
            $result         =   M('zuji_member')->where($where)->field($field)->find();
            if (empty($result))
            {
                $user_data['openid']      =   $res['openid'];
                $user_data['create_time'] =   time();
                $user_data['login_time']  =   time();
                $user_data['session_key'] =   $res['session_key'];
                $user_data['login_num']   =   1;
                $user_id                  =   M('zuji_member')->add($user_data);
            }
            else
            {
                $user_id                    =   $result['user_id'];
                $user_data['login_time']    =   time();
                $user_data['login_num']     =   $result['login_num'] + 1;
                $user_data['session_key']   =   $res['session_key'];
            }

            $user_data['user_id']           =   $user_id;
            $user_data['openid']            =   $res['openid'];

            //生成token
            $module     =   'zuji';
            $Controller =   'user';
            $Method     =   'login';
            $expire_time=   time() + 7*24*3600;
            $key        =   'yuanyi2018';
            $user_data['expire_time'] =   $expire_time;
            $user_data['token']       =   md5($module.$Controller.$Method.$expire_time.$key.$user_id);
            M('zuji_member')->where($where)->save($user_data); // 根据条件更新记录

            if (! empty($share_uid) && $share_uid != $user_id)
            {
                //朋友
                $this->make_friend($user_id ,$share_uid);
            }

            $data               =   array();
            $data['userinfo']   =   $user_data;
            $data['token']      =   $user_data['token'];
            $this->returnData($data,'ok',0);
        }
    }

    public function login_code()
    {
        $code                   =   I('post.code','','htmlspecialchars');
        $share_uid              =   I('post.share_uid','','htmlspecialchars');
        if (! empty($code))
        {
            $appid      =   'wx8e26d39e155e28ef';
            $secret     =   '0836bbd26264ffe013a12cadbae30925';
            $url        =   "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";

            $res = $this->curlget($url);

            if (!$res['openid'])
            {
                $this->returnData($res,$msg='openid获取失败',$code=1001);
            }

            $user_id        =   0;
            $user_data      =   array();
            $where          =   array();
            $where['openid']=   $res['openid'];
            $field          =   array('user_id','openid','login_num');
            $result         =   M('zuji_member')->where($where)->field($field)->find();
            if (empty($result))
            {
                $user_data['openid']      =   $res['openid'];
                $user_data['create_time'] =   time();
                $user_data['login_time']  =   time();
                $user_data['session_key'] =   $res['session_key'];
                $user_data['login_num']   =   1;
                $user_id                  =   M('zuji_member')->add($user_data);
            }
            else
            {
                $user_id                    =   $result['user_id'];
                $user_data['login_time']    =   time();
                $user_data['login_num']     =   $result['login_num'] + 1;
                $user_data['session_key']   =   $res['session_key'];
            }

            $user_data['user_id']           =   $user_id;
            $user_data['openid']            =   $res['openid'];

            //生成token
            $module     =   'zuji';
            $Controller =   'user';
            $Method     =   'login';
            $expire_time=   time() + 7*24*3600;
            $key        =   'yuanyi2018';
            $user_data['expire_time'] =   $expire_time;
            $user_data['token']       =   md5($module.$Controller.$Method.$expire_time.$key.$user_id);
            M('zuji_member')->where($where)->save($user_data); // 根据条件更新记录

            if (! empty($share_uid) && $share_uid != $user_id)
            {
                //朋友
                $this->make_friend($user_id ,$share_uid);
            }

            $field              =   array('user_id','openid','login_num','avatar','city','nickname','province','country');
            $result             =   M('zuji_member')->where($where)->field($field)->find();
            $data               =   array();
            $data['userinfo']   =   $result;
            $data['token']      =   $user_data['token'];
            $this->returnData($data,'ok',0);
        }
    }

    public function update_user_info()
    {
        $token                  =   I('post.token','','htmlspecialchars');
        $user_data              =   array();
        $user_data['avatar']    =   I('post.avatar','','htmlspecialchars');
        $user_data['city']      =   I('post.city','','htmlspecialchars');
        $user_data['gender']    =   I('post.gender','','htmlspecialchars');
        $user_data['language']  =   I('post.language','','htmlspecialchars');
        $user_data['nickname']  =   I('post.nickname','','htmlspecialchars');
        $user_data['province']  =   I('post.province','','htmlspecialchars');
        $user_data['country']   =   I('post.country','','htmlspecialchars');

        if (! empty($token ) && ! empty($user_data))
        {
            $where          =   array();
            $field          =   array('user_id','openid','perfect_time');
            $where['token'] =   $token;
            $member_data    =   M('zuji_member')->where($where)->field($field)->find();
            if (! empty($member_data))
            {
                if ($member_data['perfect_time'] == 0 || $member_data['perfect_time'] < time())
                {
                    $user_data                  = array_filter($user_data);
                    $user_data['perfect_time']  =   time() + 7*24*3600;
                    M('zuji_member')->where($where)->save($user_data);
                }
            }
        }
    }

}
